package com.example.demo.service.impl;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.CourseMap;
import com.example.demo.po.Course;
import com.example.demo.po.dto.CourseParam;
import com.example.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMap courseMap;

    @Override
    public Result SaveCourse(CourseParam courseParam) {

//        String name;
//        String time;
//        String start;

        if(StringUtils.isEmpty(courseParam.getName())||StringUtils.isEmpty(courseParam.getTime())||StringUtils.isEmpty(courseParam.getStart()))
        {
            return ResultUtil.fail("名称、时间必填！");
        }

        if(courseParam.getNumber()<=0){
            return ResultUtil.fail("必填人数！");
        }

        List<CourseParam> list =  courseMap.GetSameTimeCourse( courseParam);

        if( list.size()>0) {
            return ResultUtil.fail("时间冲突");
        }


        courseMap.SaveCourse(courseParam);
        return ResultUtil.success(courseParam);
    }

    @Override
    public void DeleteCourse(int id) {

    }

    @Override
    public Result UpdateCourse(CourseParam courseParam) {

        if(StringUtils.isEmpty(courseParam.getName())||StringUtils.isEmpty(courseParam.getTime())||StringUtils.isEmpty(courseParam.getStart()))
        {
            return ResultUtil.fail("名称、时间必填！");
        }

        if(courseParam.getNumber()<=0){
            return ResultUtil.fail("必填人数！");
        }

        List<CourseParam> list =  courseMap.GetSameTimeCourse( courseParam);
        if( list.size()>1) {
            return ResultUtil.fail("时间冲突");
        }

        System.out.println( courseParam);

        courseMap.UpdateCourse(courseParam);

        return ResultUtil.success("修改成功");
    }

    @Override
    public List<Course> GetCourse() {
        return null;
    }
}
